package com.sixks.app.mvc.cmd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sixks.app.AliExpressConstant;
import com.sixks.app.api.ApiCallService;
import com.sixks.app.domain.Category;
import com.sixks.app.domain.ExpressUser;
import com.sixks.app.mvc.cmd.exception.ExpressAPIException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author colin
 * @date: 02/03/2014
 * @time: 12:10
 */
public class CategoryById extends BaseCommandAPI<CateRequest> {

    @Override
    protected APIResponse handle(CateRequest request, ExpressUser user) {
        int cateId = 0;
        if(request != null && request.getId() != null){
            cateId = request.getId();
        }
        List<Category> result = getCate(cateId, user.getAccessToken());
        for (Category each : result) {
            System.out.println(each.getZhName() + ". ------------------------------------------------");
            printCategory(each);
        }
        APIResponse response = new APIResponse();
        return response;
    }

    void printCategory(Category category) {
        int parentId = category.getParent() == null ? 0 : category.getParent().getId();
        System.out.println("INSERT express_category Category (id,zh,en,parent) values (" + category.getId() + ",'" + category.getZhName().replaceAll("'","''") + "','" + category.getEnName().replaceAll("'","''") + "'," + parentId + ");##isleaf:"+String.valueOf(category.isIsleaf()));
        for (Category each : category.getChildren()) {
            printCategory(each);
        }
    }

    private List<Category> getCate(int id, String token) {
        List<Category> categories = new ArrayList<Category>();
        Map<String, String> params = new HashMap<String, String>();
        params.put("cateId", String.valueOf(id));
        params.put("access_token", token);
        String json = null;
        try {
            json = ApiCallService.callApiTest(HTTP_ROOT, "param2/1/aliexpress.open/api.getChildrenPostCategoryById/" + AliExpressConstant.APP_KEY, AliExpressConstant.SIGN, params);
        } catch (ExpressAPIException e) {
            e.printStackTrace();
        }
        System.out.println(json);
        Map map = JSON.parseObject(json, Map.class);
        JSONArray array = (JSONArray) map.get("aeopPostCategoryList");
        for (Object anArray : array) {
            JSONObject jobj = (JSONObject) anArray;
            Category cate = JSON.toJavaObject(jobj, Category.class);
            if(!cate.getZhName().equals("服装/服饰配件") && cate.getLevel() == 0){// || !cate.getZhName().equals("鞋子") || !cate.getZhName().equals("珠宝钟表")){      //珠宝钟表
                continue;
            }
            if (!cate.isIsleaf()) {
                cate.setChildren(getCateChild(cate.getId(), token));
            }else{
                System.out.println("----------------------- "+cate.getId());
            }
            categories.add(cate);
        }
        return categories;
    }
    private List<Category> getCateChild(int id, String token) {
        List<Category> categories = new ArrayList<Category>();
        Map<String, String> params = new HashMap<String, String>();
        params.put("cateId", String.valueOf(id));
        params.put("access_token", token);
        String json = null;
        try {
            json = ApiCallService.callApiTest(HTTP_ROOT, "param2/1/aliexpress.open/api.getChildrenPostCategoryById/" + AliExpressConstant.APP_KEY, AliExpressConstant.SIGN, params);
        } catch (ExpressAPIException e) {
            e.printStackTrace();
        }
        System.out.println(json);
        Map map = JSON.parseObject(json, Map.class);
        JSONArray array = (JSONArray) map.get("aeopPostCategoryList");
        for (Object anArray : array) {
            JSONObject jobj = (JSONObject) anArray;
            Category cate = JSON.toJavaObject(jobj, Category.class);

            if (!cate.isIsleaf()) {
                cate.setChildren(getCateChild(cate.getId(), token));
            }
            categories.add(cate);
        }
        return categories;
    }
}
